package com.yjz.ui.banner.core;

import android.content.Context;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.ViewPager;

import com.yjz.ui.R;
import com.yjz.ui.banner.BannerLayout;
import com.yjz.ui.banner.indicator.BannerIndicator;
import com.yjz.ui.banner.indicator.CircleIndicator;

import java.util.List;

/**
 * 作用 :
 * 作者 : yjz
 * 时间 : 2024/10/21 15:09
 */
public class BannerDelegate implements IBanner, ViewPager.OnPageChangeListener {

    private final Context mContext;
    private final BannerLayout mBannerLayout;
    private List<? extends BannerModel> mBannerModleList;
    private BannerAdapter mAdapter;
    private BannerIndicator<?> mIndicator;
    private boolean mAutoPlay;
    private boolean mLoop;
    private ViewPager.OnPageChangeListener mOnPageChangeListener;
    private int mIntervalTime = 1000;
    private BannerLayout.OnBannerClickListener mOnBannerClickListener;
    private AutoScrollerViewPager mAutoScrollerViewPager;
    private int mScrollDuration;

    public BannerDelegate(Context context, BannerLayout bannerLayout) {
        this.mContext = context;
        this.mBannerLayout = bannerLayout;
    }

    @Override
    public void setBannerData(int layoutResId, @NonNull List<? extends BannerModel> models) {
        mBannerModleList = models;
        init(layoutResId);
    }

    public void setAdapter(BannerAdapter adapter) {
        this.mAdapter = adapter;
    }

    @Override
    public void setBannerData(@NonNull List<? extends BannerModel> models) {
        setBannerData(R.layout.banner_item_image, models);
    }

    @Override
    public void setBannerIndicator(BannerIndicator indicator) {
        this.mIndicator = indicator;
    }

    @Override
    public void setAutoPlay(boolean autoPlay) {
        this.mAutoPlay = autoPlay;
    }

    @Override
    public void setLoop(boolean loop) {
        this.mLoop = loop;
    }

    @Override
    public void setIntervalTime(int intervalTime) {
        if (intervalTime > 0) {
            this.mIntervalTime = intervalTime;
        }
    }

    @Override
    public void setBindAdapter(IBannerBindAdapter bindAdapter) {
        if (mAdapter == null) {
            mAdapter = new BannerAdapter(mContext);
        }
        mAdapter.setBindAdapter(bindAdapter);
    }

    @Override
    public void setOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener) {
        this.mOnPageChangeListener = onPageChangeListener;
    }

    @Override
    public void setOnBannerClickListener(OnBannerClickListener onBannerClickListener) {
        this.mOnBannerClickListener = onBannerClickListener;
    }

    @Override
    public void setScrollDuration(int duration) {
        this.mScrollDuration = duration;
        if (mAutoScrollerViewPager != null && duration > 0) mAutoScrollerViewPager.setScrollDuration(duration);
    }

    private void init(int layoutResId) {

        if (mAdapter == null) {
            throw new NullPointerException("please first call setBindAdapter!");
        }

        if (mIndicator == null) {
            mIndicator = new CircleIndicator(mContext);
        }
        mIndicator.onInflate(mBannerModleList.size());
        mAdapter.setLayoutResId(layoutResId);
        mAdapter.setBannerData(mBannerModleList);
        mAdapter.setAutoPlay(mAutoPlay);
        mAdapter.setLoop(mLoop);
        mAdapter.setOnBannerClickListener(mOnBannerClickListener);

        mAutoScrollerViewPager = new AutoScrollerViewPager(mContext);
        mAutoScrollerViewPager.setIntervalTime(mIntervalTime);
        mAutoScrollerViewPager.addOnPageChangeListener(this);
        mAutoScrollerViewPager.setAutoPlay(mAutoPlay);

        if (mScrollDuration > 0) mAutoScrollerViewPager.setScrollDuration(mScrollDuration);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        mAutoScrollerViewPager.setAdapter(mAdapter);


        if ((mLoop || mAutoPlay) && mAdapter.getRealCount() != 0) {
            //无限轮播关键点：使第一张能反向滑动到最后一张，已达到无限滚动的效果
            int firstItem = mAdapter.getFirstItem();
            mAutoScrollerViewPager.setCurrentItem(firstItem, false);
        }

        //清除缓存view
        mBannerLayout.removeAllViews();
        mBannerLayout.addView(mAutoScrollerViewPager, layoutParams);
        mBannerLayout.addView(mIndicator.get(), layoutParams);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (null != mOnPageChangeListener && mAdapter.getRealCount() != 0) {
            mOnPageChangeListener.onPageScrolled(position % mAdapter.getRealCount(), positionOffset, positionOffsetPixels);
        }
    }

    @Override
    public void onPageSelected(int position) {
        if (mAdapter.getRealCount() == 0) {
            return;
        }
        position = position % mAdapter.getRealCount();
        if (mOnPageChangeListener != null) {
            mOnPageChangeListener.onPageSelected(position);
        }
        if (mIndicator != null) {
            mIndicator.onPointChange(position, mAdapter.getRealCount());
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (mOnPageChangeListener != null) {
            mOnPageChangeListener.onPageScrollStateChanged(state);
        }
    }
}
